package lab2.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import net.sourceforge.jeval.*;

public class Lab2CalcActivity extends Activity implements OnClickListener{
	private Button m_back, m_c, m_ce, m_0, m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8, m_9, m_comma, m_sub, m_divide, m_multiply, m_add, m_lPara, m_rPara, m_equal;
	private TextView m_textView;
	private String m_math = "";
	private Evaluator m_evaluator;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Create the evaluator
        m_evaluator = new Evaluator();
        
        // If one exists restore the old expression
        if (savedInstanceState != null)
        {
        	m_math = savedInstanceState.getString("math");
        }
        
        // Find the views.
        m_textView = (TextView)findViewById(R.id.CalcWindow);
        m_back = (Button)findViewById(R.id.Backstep);
        m_c = (Button)findViewById(R.id.C);
        m_ce = (Button)findViewById(R.id.CE);
        m_0 = (Button)findViewById(R.id.B0);
        m_1 = (Button)findViewById(R.id.B1);
        m_2 = (Button)findViewById(R.id.B2);
        m_3 = (Button)findViewById(R.id.B3);
        m_4 = (Button)findViewById(R.id.B4);
        m_5 = (Button)findViewById(R.id.B5);
        m_6 = (Button)findViewById(R.id.B6);
        m_7 = (Button)findViewById(R.id.B7);
        m_8 = (Button)findViewById(R.id.B8);
        m_9 = (Button)findViewById(R.id.B9);
        m_comma = (Button)findViewById(R.id.Comma);
        m_sub = (Button)findViewById(R.id.Subtract);
        m_divide = (Button)findViewById(R.id.Divide);
        m_multiply = (Button)findViewById(R.id.Multiply);
        m_add = (Button)findViewById(R.id.Add);
        m_lPara = (Button)findViewById(R.id.LeftParantheses);
        m_rPara = (Button)findViewById(R.id.RightPrantheses);
        m_equal = (Button)findViewById(R.id.Equals);
        
        // Add listeners to all the views.
        m_back.setOnClickListener(this);
        m_c.setOnClickListener(this);
        m_ce.setOnClickListener(this);
        m_0.setOnClickListener(this);
        m_1.setOnClickListener(this);
        m_2.setOnClickListener(this);
        m_3.setOnClickListener(this);
        m_4.setOnClickListener(this);
        m_5.setOnClickListener(this);
        m_6.setOnClickListener(this);
        m_7.setOnClickListener(this);
        m_8.setOnClickListener(this);
        m_9.setOnClickListener(this);
        m_comma.setOnClickListener(this);
        m_sub.setOnClickListener(this);
        m_divide.setOnClickListener(this);
        m_multiply.setOnClickListener(this);
        m_add.setOnClickListener(this);
        m_lPara.setOnClickListener(this);
        m_rPara.setOnClickListener(this);
        m_equal.setOnClickListener(this);
        
        // Check if m_math is empty if not print it in the textView
        if(m_math != "")
        {
        	m_textView.setText(m_math);
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	// TODO Auto-generated method stub
    	super.onSaveInstanceState(outState);
    	
    	outState.putString("math", m_math);
    }
    
    // Implements the interface
    public void onClick(View v)
    {
    	// Do something depending on which button was pressed.
    	switch(v.getId())
    	{
	    	case R.id.Backstep:
	    		if (m_math.length() != 0)
	    		{
	    			m_math = m_math.substring(0, m_math.length() - 1);
	    		}
	    		break;
	    	case R.id.C:
	    		m_math = "";
	    		break;
	    	case R.id.CE:
	    		break;
	    	case R.id.B0:
	    		m_math += "0";
	    		break;
	    	case R.id.B1:
	    		m_math += "1";
	    		break;
	    	case R.id.B2:
	    		m_math += "2";
	    		break;
	    	case R.id.B3:
	    		m_math += "3";
	    		break;
	    	case R.id.B4:
	    		m_math += "4";
	    		break;
	    	case R.id.B5:
	    		m_math += "5";
	    		break;
	    	case R.id.B6:
	    		m_math += "6";
	    		break;
	    	case R.id.B7:
	    		m_math += "7";
	    		break;
	    	case R.id.B8:
	    		m_math += "8";
	    		break;
	    	case R.id.B9:
	    		m_math += "9";
	    		break;
	    	case R.id.Comma:
	    		m_math += ".";
	    		break;
	    	case R.id.Subtract:
	    		m_math += "-";
	    		break;
	    	case R.id.Divide:
	    		m_math += "/";
	    		break;
	    	case R.id.Multiply:
	    		m_math += "*";
	    		break;
	    	case R.id.Add:
	    		m_math += "+";
	    		break;
	    	case R.id.LeftParantheses:
	    		m_math += "(";
	    		break;
	    	case R.id.RightPrantheses:
	    		m_math += ")";
	    		break;
	    	case R.id.Equals:
			try {
				m_math = m_evaluator.evaluate(m_math);
			} catch (EvaluationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    		break;
    	}
    	m_textView.setText(m_math);
    }
}